<?php

namespace app\tours\model;

use think\model\Relation;

class ToursOrder extends Relation {

        protected $link = array(
            'tours' => array(
                'mapping_type' => BELONGS_TO,
                'class_name' => 'Tours',
                'foreign_key' => 'tours_id',
                'mapping_fields' => 'attachpath,attachthumb,title,subtitle,price,type',
                'as_fields' => 'attachpath,attachthumb,title,subtitle,price,type',
                'parent_key' => 'id'
            )
        );

        protected function _after_select(&$resultSet, $options = array()) {
                parent::_after_select($resultSet, $options);
                foreach ($resultSet as $key => $value) {
                        $resultSet[$key]['tours_url'] = U("/xianlu/" . $value['tours_id'], '', 'html', false, true);
                        $resultSet[$key]['person_number'] = $value['adult'] + $value['child'];
                        if ($resultSet[$key]['attachthumb']) {
                                $resultSet[$key]['tours_image'] = C('imgurl') . $value['attachpath'];
                        } else {
                                $resultSet[$key]['tours_image'] = "/public/tours/images/nophoto.png";
                        }
                        $resultSet[$key]['payment'] = M("ToursPayment")->where(array('order_id' => $value['order_id']))->getField("payment");
                }
        }

        protected function _after_find(&$result, $options = array()) {
                parent::_after_find($result, $options);
                if ($result['attachthumb']) {
                        $result['tours_image'] = C('imgurl') . $result['attachpath'];
                } else {
                        $result['tours_image'] = "/public/tours/images/nophoto.png";
                }
                $result['tours_url'] = U("/xianlu/" . $result['tours_id'], 'html', 'www');
                $result['price_sum'] =  ($result['adult'] * $result['adult_price']) + ($result['child'] * $result['child_price']);
                $result['payment'] = M("ToursPayment")->where(array('order_id' => $result['order_id']))->getField("payment");
        }

}
